package cn.edu.neusoft.dnui.startproject.model;

import cn.edu.neusoft.dnui.startproject.bean.Phone91Result;
import cn.edu.neusoft.dnui.startproject.bean.PhoneResult;
import cn.edu.neusoft.dnui.startproject.iface.PhoneIface;
import cn.edu.neusoft.dnui.startproject.listener.PhoneListener;
import cn.edu.neusoft.dnui.startproject.service.Phone91Service;
import cn.edu.neusoft.dnui.startproject.service.PhoneService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Junsheng on 2016/12/14.
 */

public class Phone91Model extends PhoneModel implements PhoneIface {
    private Phone91Service service;
    private static Phone91Model model;
    private static final String BASE_URL = "http://api.91cha.com/";
    private static final String API_KEY = "5dea30ab154f4702b934ac9f178505e5";
    public Phone91Model(String url) {
        super(url);
    }
    public Phone91Model(){
        super(BASE_URL);
    }
    public static Phone91Model getInstance(){
        if(model==null){
            model = new Phone91Model();
        }
        return model;
    }

    @Override
    public void getLocation(String tel, final PhoneListener listener) {
        service = createService(Phone91Service.class);
        Call<Phone91Result> call = service.getResult(API_KEY,tel);
        call.enqueue(new Callback<Phone91Result>() {
            @Override
            public void onResponse(Call<Phone91Result> call, Response<Phone91Result> response) {
                if (response.isSuccessful()){
                    Phone91Result result = response.body();
                    if (result != null&&result.getData()!=null){
                        listener.onResponse(result.getData().getProvince());

                    }
                    else{
                        listener.onFail("解析错误！");
                    }
                }
            }

            @Override
            public void onFailure(Call<Phone91Result> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
